package com.wu.concurencys.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 这个类主要讲解了 CountDownLathch  的倒计时操作主要是用到了await() 方法
 */
@Slf4j
public class CountDownLathchExample1 {

    private  static final int threadcount = 200;
    public static void main(String[] args) throws  Exception{

        ExecutorService exec = Executors.newCachedThreadPool();
        final CountDownLatch countDownLatch = new CountDownLatch(threadcount);
        for (int i = 0;i<threadcount;i++)
        {
            final  int threadnumber = i;
            exec.execute(() ->{
               try {
                   test(threadnumber);

               }catch (Exception e)
               {
                   log.info("{}",e);
               }finally {
                   countDownLatch.countDown();
                }
            });
        }
        countDownLatch.await();  // await 保证所有的线程执行玩在执行await()
        log.info("finash*****");
        exec.shutdown();// 关闭线程池

    }

    private static void test(int number) throws  Exception
    {

        Thread.sleep(100);
        log.info("{}",number);
        Thread.sleep(100);

    }
}
